﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MiddleEnemyPlane : EnemyPlane {

	// Use this for initialization
	void Start () {
		InitPro();
		hpSlider = this.GetComponent<HPSlider> ();
		isDeath = false;
		Spawn.SetTarget += SetTarget;
	}
	
	// Update is called once per frame
	void Update () {
		RecoverHp();
		if(target != null && target.transform.position.z == 0)
		{
			distanceTarget = Vector3.Distance(transform.position, target.transform.position);
			distanceWeaponAttack = transform.Find("gun").GetComponent<EnemyGun>().GetWeapon().AttackDistance;
			transform.up = (target.transform.position - transform.position).normalized;
			// transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
			
			// 星球攻击状态
			if(star.state == Star.state_attack)
			{
				// 追踪
				if(distanceTarget > distanceWeaponAttack)
				{
					transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
				}
			}
			// 星球防御状态
			else if(star.state == Star.state_defend)
			{
				// 两倍攻击范围内追踪
				if(distanceTarget > distanceWeaponAttack && distanceTarget < distanceWeaponAttack * 2)
				{
					transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
				}
				// 大于两倍范围飞回原位
				if(distanceTarget > distanceWeaponAttack * 2)
				{
					target = null;
				}
			}
			// 星球死亡状态
			else if(star.state == Star.state_death)
			{
				// 全图追踪
				if(distanceTarget > distanceWeaponAttack)
				{
					transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
				}
			}
		}
		else
		{
			transform.up = (star.transform.position - transform.position).normalized;
			transform.position = Vector3.MoveTowards(transform.position, star.transform.position, speed * Time.deltaTime);
		}
	}

	private void InitPro()
	{
		type = EnemyPlane.Type_MiddleEnemyPlane;
		hp = 200;
		currHp = hp;
		defendPower = 5;
		defendHP = 0;
		recoverHP = 0;
		speed = 2;
		resistanceLaser = 1;
		resistanceMissile = 1;
		resistanceBullet = 1;
		startTrans = this.transform;
	}

	// 设置造船技术力并且重置属性
	public void setProducePower(int producePower)
	{
		this.producePower = producePower;
		ResetProByProducePower();
	}

	private void ResetProByProducePower()
	{
		/*
		敌机属性根据producePower变化而变化
		 */
	}

	

	




	public void TakeDamage(float damage){
		if(currHp <= 0)return;
		currHp -= damage;
		hpSlider.hpSlider.value = currHp / hp;
		if(currHp <= 0)
		{
			print("一个中型敌机死啦");
			hpSlider.hpSlider.value = 0;
			Destroy(gameObject);
		}
	}
}
